package com.thb.pay.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.thb.pay.core.utils.R;
import com.thb.pay.entity.ScheduleJobEntity;
import com.thb.pay.service.ScheduleJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 定时任务
 */
@RestController
@RequestMapping("/sys/schedule")
public class ScheduleJobController {
    @Autowired
    private ScheduleJobService scheduleJobService;

    /**
     * 定时任务列表
     */
    @GetMapping("/list/{pageNumber}/{pageSize}")
    public R list(@PathVariable("pageNumber") Integer pageNumber, @PathVariable("pageSize") Integer pageSize) {
        //查询列表数据
        IPage<ScheduleJobEntity> scheduleJobPage = scheduleJobService.page(new Page<ScheduleJobEntity>(pageNumber, pageSize), null);
        return R.ok().put("data", scheduleJobPage);
    }

    /**
     * 定时任务信息
     */
    @GetMapping("/info/{jobId}")
    public R info(@PathVariable("jobId") Long jobId) {
        ScheduleJobEntity schedule = scheduleJobService.queryObject(jobId);

        return R.ok().put("data", schedule);
    }

    /**
     * 保存定时任务
     */
    @PostMapping("/save")
    public R save(@RequestBody ScheduleJobEntity scheduleJob) {
        scheduleJobService.save(scheduleJob);
        return R.ok();
    }

    /**
     * 修改定时任务
     */
    @PostMapping("/update")
    public R update(@RequestBody ScheduleJobEntity scheduleJob) {
        scheduleJobService.update(scheduleJob);
        return R.ok();
    }

    /**
     * 删除定时任务
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] jobIds) {
        scheduleJobService.deleteBatch(jobIds);
        return R.ok();
    }

    /**
     * 立即执行任务
     */
    @PostMapping("/run")
    public R run(@RequestBody Long[] jobIds) {
        scheduleJobService.run(jobIds);
        return R.ok();
    }

    /**
     * 立即执行任务
     */
    @GetMapping("/run/{jobId}")
    public R run(@PathVariable("jobId") Long jobId) {
        List<Long> jobList = Lists.newArrayList();
        jobList.add(jobId);
        scheduleJobService.run(jobList.toArray(new Long[jobList.size()]));
        return R.ok();
    }

    /**
     * 暂停定时任务
     */
    @RequestMapping("/pause")
    public R pause(@RequestBody Long[] jobIds) {
        scheduleJobService.pause(jobIds);
        return R.ok();
    }

    /**
     * 恢复定时任务
     */
    @RequestMapping("/resume")
    public R resume(@RequestBody Long[] jobIds) {
        scheduleJobService.resume(jobIds);
        return R.ok();
    }

    @GetMapping("/test")
    public R test() {
        ScheduleJobEntity scheduleJob = new ScheduleJobEntity();
        scheduleJob.setBeanName("testScheduleTask");
        scheduleJob.setMethodName("sayHello");
        scheduleJob.setRemark("test");
        scheduleJob.setCronExpression("0 0/30 * * * ?");
        scheduleJob.setCreateTime(new Date());
        scheduleJob.setParams("xxxxxxxxxxxxxxxxxxxxxxxxx");
        scheduleJobService.save(scheduleJob);
        return R.ok();
    }

}
